<?php

namespace App\Handler;

use App\Models\Menu;
use App\Utils\Tree\Tree;
use App\Utils\Tree\TreeNode;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/12
 * Time: 9:36
 */
class MenuHandler
{

    static public function resetOrderNumbers()
    {
        /**
         * @var  $item Menu
         */
        $query = Menu::orderBy("system_flag", "asc")->orderBy("order_number", "asc");
        $list = $query->get();
        $index = 0;
        $orderNumber = 0;
        foreach ($list as $item) {
            $parentId = $item->get_parent_id();
            if (empty($parentId)) {
                $index++;
                $orderNumber = 1000 * $index;
            }else{
                $orderNumber += 20 ;
            }
            $item->set_order_number($orderNumber);
            $item->update();
        }

    }

    static public function toAdminDesignMenus($menus)
    {
        /**
         * @var $menu Menu
         */
        $tree = new Tree("menu.tree", "menus");
        foreach ($menus as $menu) {
            $id = $menu["menu_id"];
            $pid = $menu["parent_id"];
            $node = new TreeNode($id, $menu["menu_name"], $pid);
            $node->setAttribute("label", $menu["menu_name"]);
            $node->setAttribute("iconClasses", $menu["icon"]);
            $node->setAttribute("url", $menu["url"]);
            $tree->add($node);
        }
        $tree->init();
        $bean = $tree->toArray();
        return $bean["children"];
    }


}